﻿; 候选项翻页
MoreWait:
	If (waitnum*ListNum+ListNum<Max(jichu_for_select_Array.Length(),Function_for_select.Length())){
		waitnum+=1
		Gosub srf_tooltip_fanye
	}
Return
lessWait:
	If (waitnum>0){
		waitnum-=1
		Gosub srf_tooltip_fanye
	}
Return

; 查字典功能跳转
srf_tooltip:
	If InStr(srf_all_Input, func_key){
		RegExMatch(srf_all_Input, "O)^" func_key "?([a-zA-Z]+)" func_key, modeMatch)
		If (srf_all_Input_history!=srf_all_Input)
			srf_all_Input_history:=srf_all_Input
		srf_for_select:=srf_all_Input_raw_for_trim:="", jichu_for_select_Array:=[], hotstring_for_select:=[]
		Switch srf_FuncSelect(srf_all_Input)
		{
			Case 1:
				Gosub srf_tooltip_fanye
			Case 0:
				Gosub showhouxuankuang
			Case -1:
				Gosub srf_value_off
		}
		Return
	} If Englishmode||(srf_all_Input~="^'"){
		jichu_for_select_Array:=get_word_lianxiang(DB, LTrim(srf_all_Input_tooltip:=srf_all_Input,"'"), "English"), srf_all_Input_history:=srf_all_Input
	} Else {
		If (srf_all_Input_history==srf_all_Input)
			Goto srf_tooltip_fanye
		Else {
			srf_all_Input_history:=srf_all_Input
		}
		Function_for_select:=[]
		customs_for_select:=get_word(DB, srf_all_Input, "customs")
		; If StrLen(srf_all_Input)>2
		; 	English_for_select:=get_word_lianxiang(DB, srf_all_Input, "English", 2, 0)
		; Else
		; 	English_for_select:=""
		If (customs_for_select[1,3]=0){
			If InStr(customs_for_select[1,0],"Rewrite"){
				srf_all_Input:=customs_for_select[1,2], customs_for_select:=get_word(DB, srf_all_Input, "customs")
			} Else {
				If InStr(customs_for_select[1,0],"Func"){
					srf_Func_Params:=StrSplit(RTrim(customs_for_select[1,2],func_key),func_key)
					If (srf_func_table[srf_Func_Params[1]]){
						value:=srf_Func_Params[1], srf_Func_Params.RemoveAt(1)
						customs_for_select[1,2]:=FuncCall(srf_func_table[value],srf_Func_Params*)
					}
					customs_for_select[1,0]:=StrReplace(customs_for_select[1,0],"{Func}")
				}
				SendInput(customs_for_select[1,2],customs_for_select[1,0])
				Goto srf_value_off
			}
		}
		Result:=%shurulei%method(srf_all_Input, Inputscheme)
		If (Result=0)
			Return
		Else If IsObject(Result)
			jichu_for_select_Array:=Result
		Else
			jichu_for_select_Array:=[]

		If (magicstring)
			hotstring_for_select:=get_word_lianxiang(DB, srf_all_Input, "hotstrings")
		If (superrun)
			Function_for_select:=get_word_lianxiang(DB, srf_all_Input, "functions")
		; RunTime.Push(["Search",A_TickCount-RunTime[1,2]])
		; If (customs_for_select.Length()=0&&fzm&&(tvar:=restoreinput(jichu_for_select_Array[1,1],fzm))){
		; 	customs_for_select:=get_word(DB, tvar, "customs")
		; 	Loop % customs_for_select.Length()
		; 		customs_for_select[A_Index,1]:=jichu_for_select_Array[1,1], customs_for_select[A_Index,-2]:=fzm
		; }
		Gosub houchuli
		; RunTime.Push(["Qucong",A_TickCount-RunTime[1,2]])
	}
	If (Result=-1)&&(!jichu_for_select_Array.Length())
		Goto srf_value_off
	Gosub srf_tooltip_fanye
	; Timelist:=""
	; Loop % RunTime.Length()-1
	; 	Timelist .= RunTime[A_Index+1,1] " " RunTime[A_Index+1,2] "|"
	; OutputDebug % Timelist "Final " A_TickCount-RunTime[1,2]
Return

; 去重 简转繁
houchuli:
	If (Traditional){
		Index:=0
		Loop {
			Index++
			If ((Index>jichu_for_select_Array.Length())||(jichu_for_select_Array[Index,2]=""))
				Break
			If tradstr:=Trim(simp2trad(jichu_for_select_Array[Index,2]), Chr(2)){
				If InStr(tradstr, Chr(2)){
					tradstr:=StrSplit(tradstr, Chr(2))
					tarr:={}
					For key,value In jichu_for_select_Array[Index]
						tarr[key]:=value
					jichu_for_select_Array[Index,5]:=tradstr[1]
					Loop % tradstr.Length()-1
						Index++, jichu_for_select_Array.InsertAt(Index, tarr), jichu_for_select_Array[Index,5]:=tradstr[A_Index+1]
				} Else
					jichu_for_select_Array[Index,5]:=tradstr
			} Else 
				jichu_for_select_Array[Index,5]:=jichu_for_select_Array[Index,2]
		}
	}
	;去重
	Loopindex:=1, qucongtable:={}
	While (Loopindex < jichu_for_select_Array.Length()+1){
		If (qucongtable[jichu_for_select_Array[Loopindex, valueindex]]){
			jichu_for_select_Array.RemoveAt(Loopindex)
			Continue
		} Else If (jichu_for_select_Array[Loopindex, valueindex])
			qucongtable[jichu_for_select_Array[Loopindex, valueindex]]:=Loopindex
		Loopindex++
	}
	Loop % (Loopindex:=customs_for_select.Length()){
		If InStr(customs_for_select[Loopindex-A_Index+1,0],"Func"){
			srf_Func_Params:=StrSplit(RTrim(customs_for_select[Loopindex-A_Index+1,2],func_key),func_key)
			If (srf_func_table[srf_Func_Params[1]]){
				value:=srf_Func_Params[1], srf_Func_Params.RemoveAt(1)
				customs_for_select[Loopindex-A_Index+1,valueindex]:=FuncCall(srf_func_table[value],srf_Func_Params*)
			}
			customs_for_select[Loopindex-A_Index+1,0]:=StrReplace(customs_for_select[Loopindex-A_Index+1,0],"{Func}")
		} Else {
			If qucongtable[customs_for_select[Loopindex-A_Index+1,2]]
				customs_for_select[Loopindex-A_Index+1,1]:=(jichu_for_select_Array.Delete(qucongtable[customs_for_select[Loopindex-A_Index+1,2]]))[1]
		}
	}
	Loop % (Loopindex:=jichu_for_select_Array.Length())
		If (jichu_for_select_Array[Loopindex+1-A_Index]="")
			jichu_for_select_Array.RemoveAt(Loopindex+1-A_Index)
	Loop % (Loopindex:=customs_for_select.Length())
		Try jichu_for_select_Array.InsertAt(Min(Max(customs_for_select[Loopindex-A_Index+1,3]-(Loopindex-A_Index),1),jichu_for_select_Array.Length()+1),customs_for_select[Loopindex-A_Index+1])
	; qucongtable:=""
Return

; 显示前处理
srf_tooltip_fanye:
	srf_for_select:=""
	If !InStr(srf_all_Input,func_key){
		If srf_all_Input&&(jichu_for_select_Array[1,valueindex]="")
			jichu_for_select_Array[1,1]:="", jichu_for_select_Array[1,valueindex]:=srf_symbol[srf_all_Input,2-EnSymbol]?srf_symbol[srf_all_Input,2-EnSymbol]:LTrim(srf_all_Input,"'")
		If (waitnum=0&&jichu_for_select_Array[2,0]="<Cloud>|-1")
			jichu_for_select_Array[2,valueindex] .= "☁"
		Switch shurulei
		{
			Case "pinyin":
				If (Textdirection="Horizontal")
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						srf_for_select.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[tvar:=ListNum*waitnum+A_Index, valueindex] (StrLen(jichu_for_select_Array[tvar, -2])<2?" " RegExReplace(jichu_for_select_Array[tvar, 6],"i)" RegExReplace(jichu_for_select_Array[tvar,-2],"(.)","(.*?)?$1"),,,1):"")
				Else
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						srf_for_select.="`n" Mod(A_Index,10) "." jichu_for_select_Array[tvar:=ListNum*waitnum+A_Index, valueindex] (StrLen(jichu_for_select_Array[tvar, -2])<2?" " RegExReplace(jichu_for_select_Array[tvar, 6],"i)" RegExReplace(jichu_for_select_Array[tvar,-2],"(.)","(.*?)?$1"),,,1):"")
			Case "sanma":
				If (ShowCode){
					If (Textdirection="Horizontal")
						Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
							srf_for_select.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] (srf_all_Input~=StrReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1]," "," ?")?"":jichu_for_select_Array[ListNum*waitnum+A_Index, 1])
					Else
						Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
							srf_for_select.="`n" Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] (srf_all_Input~=StrReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1]," "," ?")?"":jichu_for_select_Array[ListNum*waitnum+A_Index, 1])
				} Else {
					If (Textdirection="Horizontal")
						Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
							srf_for_select.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex]
					Else
						Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
							srf_for_select.="`n" Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex]
				}
			Case "lianda":
				If (Textdirection="Horizontal")
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						srf_for_select.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[tvar:=ListNum*waitnum+A_Index, valueindex] (StrLen(jichu_for_select_Array[tvar, -2])<2?" " RegExReplace(jichu_for_select_Array[tvar, 6],"i)" RegExReplace(jichu_for_select_Array[tvar,-2],"(.)","(.*?)?$1"),,,1):"")
				Else
					Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
						srf_for_select.="`n" Mod(A_Index,10) "." jichu_for_select_Array[tvar:=ListNum*waitnum+A_Index, valueindex] (StrLen(jichu_for_select_Array[tvar, -2])<2?" " RegExReplace(jichu_for_select_Array[tvar, 6],"i)" RegExReplace(jichu_for_select_Array[tvar,-2],"(.)","(.*?)?$1"),,,1):"")
			Default:
				If (ShowCode||zigen){
					If (Textdirection="Horizontal")
						Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
							srf_for_select.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] ((jichu_for_select_Array[ListNum*waitnum+A_Index, 0]~="pinyin|<Cloud>")?pinyinfancha[jichu_for_select_Array[ListNum*waitnum+A_Index, 2]]:jichu_for_select_Array[ListNum*waitnum+A_Index, 1]~="^" jichu_for_select_Array[0]?(Trim(var:=RegExReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1], "^" jichu_for_select_Array[0], " "))?var:(zigen?xingmazigen(jichu_for_select_Array[ListNum*waitnum+A_Index, 2], Inputscheme):"")):(zigen?xingmazigen(jichu_for_select_Array[ListNum*waitnum+A_Index, 2], Inputscheme):""))
					Else
						Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
							srf_for_select.="`n" Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] ((jichu_for_select_Array[ListNum*waitnum+A_Index, 0]~="pinyin|<Cloud>")?pinyinfancha[jichu_for_select_Array[ListNum*waitnum+A_Index, 2]]:jichu_for_select_Array[ListNum*waitnum+A_Index, 1]~="^" jichu_for_select_Array[0]?(Trim(var:=RegExReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1], "^" jichu_for_select_Array[0], " "))?var:(zigen?xingmazigen(jichu_for_select_Array[ListNum*waitnum+A_Index, 2], Inputscheme):"")):(zigen?xingmazigen(jichu_for_select_Array[ListNum*waitnum+A_Index, 2], Inputscheme):""))
				} Else {
					If (Textdirection="Horizontal")
						Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
							srf_for_select.=A_Space A_Space Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] (InStr(jichu_for_select_Array[ListNum*waitnum+A_Index, 0],Inputscheme "|")&&jichu_for_select_Array[ListNum*waitnum+A_Index, 1]~="^" jichu_for_select_Array[0]?RegExReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1], "^" jichu_for_select_Array[0], " "):"")
					Else
						Loop % Min(jichu_for_select_Array.Length()-ListNum*waitnum, ListNum)
							srf_for_select.="`n" Mod(A_Index,10) "." jichu_for_select_Array[ListNum*waitnum+A_Index, valueindex] (InStr(jichu_for_select_Array[ListNum*waitnum+A_Index, 0],Inputscheme "|")&&jichu_for_select_Array[ListNum*waitnum+A_Index, 1]~="^" jichu_for_select_Array[0]?RegExReplace(jichu_for_select_Array[ListNum*waitnum+A_Index, 1], "^" jichu_for_select_Array[0], " "):"")
				}
		}
		If (waitnum=0&&jichu_for_select_Array[2,0]="<Cloud>|-1")
			jichu_for_select_Array[2,valueindex]:=SubStr(jichu_for_select_Array[2,valueindex],1,-1)
		If (hotstring_for_select[1, 1])
			srf_for_select .= "`n(,)" hotstring_for_select[1, 3]
		If (Function_for_select[1, 1])
			srf_for_select .= "`n(.)" Function_for_select[1, 3]
		If (Inputscheme~="pinyin|sp$")&&(jichu_for_select_Array[1,0]~="^(pinyin|customs)"){
			ToolTipText:=StrReplace(Trim(srf_all_Input_raw_for_trim,"'"), "'\'", " ")
			If (fzm)
				ToolTipText:=RegExReplace(ToolTipText, RegExReplace(fzm,"(.)","'?$1") "$", " " fzm)
			ToolTipText:=insertcaret(ToolTipText,insertpos)
			If (Showquanpin&&!InStr(srf_all_Input,func_key))
				ToolTipText .= "`n[ " StrReplace(srf_all_Input_tooltip,"''"," ") " ]"
		} Else If (jichu_for_select_Array[1,0]~="^English"){
			ToolTipText:=insertcaret(srf_all_Input,insertpos)
		} Else {
			ToolTipText:=""
			If (shurulei~="sanma|lianda"){
				Loop % save_field_array.Length()
					ToolTipText .= save_field_array[A_Index,0] " "
				ToolTipText .= RegExReplace(srf_all_Input,"^" StrReplace(ToolTipText, " ", " ?"))
				If (fzm)
					ToolTipText:=RegExReplace(ToolTipText, " ?" fzm " ?$", " " fzm)
				If (ToolTipText="")
					ToolTipText:=srf_all_Input_tooltip (SubStr(srf_all_Input,0)="'"?"'":SubStr(srf_all_Input,0)="\"?" ":""), ToolTipText:=RegExReplace(ToolTipText, "''", " ")
			} Else
				ToolTipText:=srf_all_Input_tooltip (SubStr(srf_all_Input,0)="'"?"'":SubStr(srf_all_Input,0)="\"?" ":""), ToolTipText:=RegExReplace(ToolTipText, "''", " ")
			ToolTipText:=insertcaret(ToolTipText,insertpos)
		}
		srf_for_select:=Trim(srf_for_select, "`n ")
	} Else If (srf_func_table[modeMatch.Value[1], "Name"]="get_hotstring"){
		Loop % Min(Function_for_select.Length()-ListNum*waitnum, ListNum)
			If (Textdirection="Horizontal")
				srf_for_select .= A_Space A_Space Mod(A_Index,10) "." Function_for_select[ListNum*waitnum+A_Index, 3]
			Else
				srf_for_select .= "`n" Mod(A_Index,10) ". " Function_for_select[ListNum*waitnum+A_Index, 3]
		srf_for_select:=Trim(srf_for_select, "`n "), ToolTipText:=insertcaret(srf_all_Input,insertpos)
	} Else If (srf_all_Input~="^" func_key "[a-z]+$"){
		Loop % Min(Function_for_select.Length()-ListNum*waitnum, ListNum)
			If (Textdirection="Horizontal")
				srf_for_select .= A_Space A_Space Mod(A_Index,10) "." Function_for_select[ListNum*waitnum+A_Index, 1]
			Else
				srf_for_select .= "`n" Mod(A_Index,10) ". " Function_for_select[ListNum*waitnum+A_Index, 1]
		srf_for_select:=Trim(srf_for_select, "`n "), ToolTipText:=insertcaret(srf_all_Input,insertpos)
	} Else {
		If (srf_all_Input~="\d"&&Function_for_select[1,1])
			Loop % Min(Function_for_select.Length()-ListNum*waitnum, ListNum)
				If (Textdirection="Horizontal")
					srf_for_select .= A_Space A_Space Chr(A_Index+96) "." Function_for_select[ListNum*waitnum+A_Index, 3]
				Else
					srf_for_select .= "`n" Chr(A_Index+96) "." Function_for_select[ListNum*waitnum+A_Index, 3]
		Else
			Loop % Min(Function_for_select.Length()-ListNum*waitnum, ListNum)
				If (Textdirection="Horizontal")
					srf_for_select .= A_Space A_Space Mod(A_Index,10) "." Function_for_select[ListNum*waitnum+A_Index, 3]
				Else
					srf_for_select .= "`n" Mod(A_Index,10) "." Function_for_select[ListNum*waitnum+A_Index, 3]
		srf_for_select:=Trim(srf_for_select, "`n "), ToolTipText:=insertcaret(srf_all_Input,insertpos)
	}

; 显示候选项
showhouxuankuang:
	If (ToolTipText=""&&srf_for_select=""){
		If (ToolTipStyle=1)
			ToolTip(1,"") ;, ToolTip(2,"")
		Return
	}
	If !(Lockedposition||hasCaretPos)
		Caret:=GetCaretPos(), hasCaretPos:=1
	If (ToolTipStyle=1){
		If (Lockedposition){
			ToolTip(1, ToolTipText "`n" srf_for_select, "x" Caret.x " y" Caret.y+30)
		} Else {
			ToolTip(1, ToolTipText "`n" srf_for_select, "x" Caret.x " y" Caret.y+30)
		}
	} Else {
		GdipText(ToolTipText, srf_for_select, Caret.X, Caret.Y+30, srf_all_Input~="^" func_key "[a-z]+$"?SymbolFont:TextFont)
	}
Return
; 自学习
zixuexi:
	If (shurulei="pinyin"){
		DB.GetTable("SELECT ROWID FROM 'pinyin' WHERE jp=""" RegExReplace(srf_last_input[2, 1],"([a-z])[a-z]*","$1") """ AND key='" StrReplace(srf_last_input[2, 1],"'","''") "' AND value='" srf_last_input[2, 2] "' LIMIT 1", Result)
		If (!Result.RowCount)
			DB.Exec("INSERT INTO 'main'.'pinyin' VALUES (""" RegExReplace(srf_last_input[2, 1],"([a-z])[a-z]*","$1") """,""" srf_last_input[2, 1]
				. """,'" srf_last_input[2, 2] "',ifnull((SELECT max(weight)+1 FROM 'pinyin' " (srf_last_input[2, 3]
				?RegExReplace(srf_last_input[2, 3], "i)^.+?(WHERE jp=.+)(ORDER BY|UNION ALL).+", "$1")
				:"WHERE jp=""" RegExReplace(srf_last_input[2, 1],"([a-z])[a-z]*","$1") """ AND key='" StrReplace(srf_last_input[2, 1],"'","''") "'") "),5000));")
	} Else If (shurulei="lianda"){
		srf_last_input[2, 0]:=StrReplace(srf_last_input[2, 1],"'")
		DB.GetTable("SELECT ROWID FROM '" Inputscheme "' WHERE key='" srf_last_input[2, 1] "' AND value='" srf_last_input[2, 2] "' LIMIT 1", Result)
		If (!Result.RowCount)
			DB.Exec("INSERT INTO 'main'.'" Inputscheme "' VALUES ('" srf_last_input[2, 1] "','" srf_last_input[2, 2] "',ifnull((SELECT max(weight)+1 FROM '" Inputscheme "' WHERE key='" srf_last_input[2, 1] "'),5000));")
	} Else If (shurulei="xingma"){
		Switch StrLen(srf_last_input[2, 2]){
			Case 0,1:
				Return
			Case 2:
				srf_last_input[2, 0]:=RegExReplace(srf_last_input[2, 1],"^([a-z][a-z])[a-z]*'([a-z][a-z])[a-z]*$","$1$2")
			Case 3:
				srf_last_input[2, 0]:=RegExReplace(srf_last_input[2, 1],"^([a-z])[a-z]*'([a-z])[a-z]*'([a-z][a-z])[a-z]*$","$1$2$3")
			Default:
				srf_last_input[2, 0]:=RegExReplace(srf_last_input[2, 1],"^([a-z])[a-z]*'([a-z])[a-z]*'([a-z])[a-z]*'.+'([a-z])[a-z]*$","$1$2$3$4")
		}
		If (srf_last_input[2, 0]=""||InStr(srf_last_input[2, 0],"'"))
			Return
		DB.GetTable("SELECT ROWID FROM '" Inputscheme "' WHERE key='" srf_last_input[2, 0] "' AND value='" srf_last_input[2, 2] "' LIMIT 1", Result)
		If (!Result.RowCount)
			DB.Exec("INSERT INTO 'main'.'" Inputscheme "' VALUES ('" srf_last_input[2, 0] "','" srf_last_input[2, 2] "',ifnull((SELECT max(weight)+1 FROM '" Inputscheme "' WHERE key='" srf_last_input[2, 0] "'),5000));")
	}
	If (DB.ErrorCode)
		DebugOutput((A_ThisLabel?"标签:" A_ThisLabel "|":A_ThisFunc?"函数:" A_ThisFunc "|":"") "消息: " DB.ErrorMsg "|SQL: " DB.SQL, DebugLevel)
Return
; 关闭候选框
srf_value_off:
	If (ToolTipStyle=1)
		ToolTip(1) ;, ToolTip(2)
	Else {
		GdipText("")
		SetTimer, waitspace, Off
	}
	If (srf_last_input[1,2]){
		srf_last_input[2]:=srf_last_input[1], srf_last_input[1]:=[]
		If (Learnfg+InStr(srf_all_Input,"\")>1&&!(A_ThisHotkey~="Esc|Enter|BackSpace")&&StrLen(srf_last_input[2, 2])<9){
			srf_last_input[2, 3]:=SQL_buffer[srf_last_input[2, 0]], srf_last_input[2, 1]:=IsPinyin?"":srf_last_input[2, 1]
			SetTimer, zixuexi, -10
		}
	}
	insertpos:=waitspace:=srf_inputing:=Learnfg:=hasCaretPos:=waitnum:=IsPinyin:=dwselect:=0
	, srf_all_Input_tooltip:=ToolTipText:=srf_all_Input_history:=srf_all_Input:=srf_for_select:=tfzm:=srf_all_Input_raw_for_trim:=""
	, Function_for_select:=[], hotstring_for_select:=[], history_field_array:=[], jichu_for_select_Array:=[]
	, modeMatch:=[], SQL_buffer:=[], save_field_array:=[]
Return